<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once('server_validations.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');	
	
	session_start();
	check_permissions(CT_PERM_ADMIN_ABONAMENT, "stari_plati.php");
 

    $db = db_connect();
    $id_client = $_SESSION['user_logat']->id_client;
	
    if ($_SESSION['client_logat']->ciclu_plata == CT_CICLU_PLATA_1) 
        $ciclu = 3;
	else 
		if ($_SESSION['client_logat']->ciclu_plata == CT_CICLU_PLATA_2) 
            $ciclu = 6;
		else 
			$ciclu = 12;
			
    $tip_abon = $_SESSION['client_logat']->tip_abon;
    
    $pret = 0;
    switch ($_SESSION['client_logat']->tip_abon) {
        case 0: 
            $pret = CT_ABONAMENT_TIP_1;
            break;
        case 1: 
            $pret = CT_ABONAMENT_TIP_2;
            break;
        case 2:
            $pret = CT_ABONAMENT_TIP_3;
            break;        
    }
    $reducere = 0;
    if ($ciclu / 6 == CT_CICLU_PLATA_2) 
        $reducere = $pret / 2;
         
    if ($ciclu / 6 == CT_CICLU_PLATA_3) 
        $reducere = $pret * 2; 
    
    $pret_cu_reducere = $pret * $ciclu;
    if ($_SESSION['client_logat']->client_reducere == CT_ABONAMENT_REDUS) {
        $reducere = $reducere + ($pret_cu_reducere / 10);
    }
    
    $suma = $pret * $ciclu;

    if (isset($_POST['plata'])) {
        $dates = Client::get_perioada_plata($db, $id_client, $ciclu);
        
        try {
            // Punem moneda by default RON = 1
            Client::insert_plata_inca_neachitata($db, $id_client, $dates[0], $dates[1], $suma - $reducere, 1);
        }
        catch (QueryInsertPlataNeachitataException $e) {
            redirectOnError(CT_STARI_PLATI_INSERT_PLATA_NEACHITATA);
        }
  
        output_notice_msg();
        if (isset($_SESSION['error_msg']) && $_SESSION['error_msg'] == 'LANG_Trebuie_sa_platiti') {
        	$_SESSION['client_logat']->rau_platnic = CT_RAU_PLATNIC_PENDING;
        	error_msg("LANG_Trebuie_sa_asteptati_pana_cand_vom_valida_plata", "home.php"); 
        	      
        }        
        if (isset($_SESSION['error_msg']) && $_SESSION['error_msg'] == 'LANG_va_apropiati_de_depasire') {
        	$_SESSION['client_logat']->rau_platnic = CT_RAU_PLATNIC_FALSE;
        	notice_msg("LANG_Trebuie_sa_asteptati_pana_cand_vom_valida_plata", "stari_plati.php"); 
        	         	
        }      	 

        if (isset($_SESSION['error_msg']) && $_SESSION['error_msg'] == 'LANG_dupa_15_zile_inchide_robinetul') {
        	$_SESSION['client_logat']->rau_platnic = CT_RAU_PLATNIC_FALSE;
        	notice_msg("LANG_Trebuie_sa_asteptati_pana_cand_vom_valida_plata", "stari_plati.php"); 
        	                	
        }        	      
    }
	html_header("LANG_Stare_Plati", null, "stari_plati", null, "stari_plati.php");	        
    
    $linkuri = array(LANG_Home => "home.php", LANG_Stare_Plati => "");         
    titlu_pagina($linkuri);  
    
	output_error_msg();
	output_notice_msg();
    output_extra_text(LANG_EXTRA_STARI_PLATI);
?>
	<div align="left" class="container">
		<input type="button" class="buton" name="printContr" value="<?php echo LANG_Listeaza_contract; ?>" onclick="javascript: document.location='printContract.php'" />
	</div>			
    <div align="left" class="container">
        <?php
            $pagina = 0;
            $per_pag = 10;
            
            render_plati_client($db, $pagina, $per_pag, $id_client, $ciclu, $_SESSION['client_logat']->tip_abon);
			
            echo "<br />";
            try {                
                $afis = Client::determine_need_to_pay($db, $id_client);
//                var_dump($_SESSION['client_logat']->rau_platnic);
                if ($afis == CT_RESULT_OK) {
                    $dates = Client::get_perioada_plata($db, $id_client, $ciclu);
                    // sunt in peridoada de CT_ZILE_DISPONIBILE_PT_PLATA in care trebuie ca clientul sa plateasca
    
                    echo '<table id="wrapper" width=\"700\">';
                        echo "<form action=\"stari_plati.php\" name=\"form3\" method=\"post\">";
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th>" . LANG_Urmatoarea_Plata . "</th>";                            
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                                echo "<tr>";
                                    echo "<td>" . LANG_Abonamentul_dvs_de_tip;
                                    if ($_SESSION['client_logat']->max_useri == CT_USERI_ABON_TIP_3)
                                    	echo " <b>" . LANG_Numar_utilizatori_nelimitat . "</b>";
                                    else echo " <b>" . $_SESSION['client_logat']->max_useri . "</b> " . LANG_utilizatori;
                                    echo "<br /> " . LANG_Cu_un_ciclu_al_platii_la . " <b>" . $ciclu . "</b> " . LANG_Luni . "</td>";
                                echo "</tr>";                        
                                echo "<tr>";
                                    echo "<td>" . LANG_Urm_Plata_va_acoperi_functionarea_programului . " <b>" . getDateForUser($dates[0]) . "</b> " . LANG_Pana_in . " <b>" . getDateForUser($dates[1]) . "</b></td>";
                                echo "</tr>";
                                echo "<tr>";                                	
                                	if ($reducere > 0)
                                    	echo "<td>" . LANG_Suma_totala_de_plata_este . " <b>" . $suma . " - " . $reducere . " = " . ($suma - $reducere)  . " RON</b></td>";
                                    else echo "<td>" . LANG_Suma_totala_de_plata_este . " <b>" . $suma . " RON</b></td>";                                    
                                echo "</tr>";                            
                                echo "<tr><td>" . LANG_Detalii_plata_factura . "</td></tr>";
                                echo "<tr>";
                                    echo "<td><input type=\"submit\" value=\"" . LANG_efectueza_plata . "\" name=\"plata\" /></td>";
                                echo "</tr>";                                                                                    
                            echo "</tbody>";
                        echo "</form>";
                    echo '</table>';                                            
                }
                if ($afis == CT_PERIOADA_PROBA) {
                    echo '<table id="wrapper" width=\"700\">';
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th align=\"center\">" . LANG_Nu_este_nevoie_de_plata . "</th>";                            
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                                echo "<tr>";
                                    echo "<td align=\"center\">" . LANG_sunteti_in_perioada_de_gratuitate . "</td>";
                                echo "</tr>";                        
                            echo "</tbody>";
                        echo "</form>";
                    echo '</table>';                	    
                }
            }
            catch (QueryCheckPerioadaPlataException $e) {
                redirectOnError(CT_STARI_PLATI_CHECK_PERIOADA_PLATA);
            }
            catch (QueryDetermineNeedToPayException $e) {
            	redirectOnError(CT_STARI_PLATI_DETERMINE_NEED_TO_PAY);
            }
            catch (QueryDetermineRauPlatnicException $e) {
            	redirectOnError(CT_STARI_PLATI_DETERMINE_NEED_TO_PAY);
            }
        ?>
    </div>
<?php

    unset($_SESSION['wrong_ids']);
    if (isLoggedIn()) html_footer();
?>